const path = require('path')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')

module.exports = {
	entry: './src/entrys/entry_cache.js',
	output: {
		filename: '[name].[contenthash].js', // 启用contenthash缓存
		path: path.resolve(__dirname, '../dists/contentHash')
	},
	plugins: [
		new CleanWebpackPlugin({
			cleanStaleWebpackAssets: true // true清理所有文件， false只删除变动过的文件(如执行 npm run watch时dist下的index.html时没有改变的，不需要删除)
		}),
		new HtmlWebpackPlugin({
			title: 'contenthash缓存'
		}),
		new webpack.HashedModuleIdsPlugin()
	],
	optimization: {
		runtimeChunk: 'single', // 将 runtime 代码拆分为一个单独的 chunk
		splitChunks: { // 将第三方库(library)（例如 lodash 或 react）提取到单独的 vendor chunk 文件中
			cacheGroups: {
				vender: {
					test: /[\\/]node_modules[\\/]/,
					name: 'venders',
					chunks: 'all'
				}
			}
		}
	}
}
